package client_tree;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import utils.Constants;
import utils.ImageCreator;
import client_gui.ClientWindow;


/**
 * Klasa koja predstavlja predefinisani TreeNodeCellRenderer. Omogucava da se svakom cvoru
 * u stablu setuje odgovarajuca ikonica i nacin prikaza, u zavisnosti od statusa korisnika
 * i vrste cvora.
 * 
 * @author UrosKrkic & Radivoje Popovic
 */
public class ContactsTreeNodeCellRenderer extends DefaultTreeCellRenderer implements Constants {

	
	private ClientWindow owner = null;
	
	
	/* 
	 * Niz sa ikonicama, indeksira se kodovima statusa.
	 * Umanjuje se za jedan, jer invisible status nema poseban prikaz, vec se prikazuje
	 * kao offline.
	 */
	private ImageIcon[] icons = new ImageIcon[STATUS_NAMES.length];

	private ImageIcon onlineIcon;
	private ImageIcon offlineIcon;
	
	// font koji se koristi za ispis cvorova stabla
	private String fontName = "Tahoma";

	// velicina fonta koji se koristi za ispis cvorova stabla
	private int fontSize = 16;
	
	// boja stabla, odnosno boja pozadine cvora
	private Color treeColor = null;
	

	
	/**
	 * Konstruktor.
	 * @param treeColor - boja stabla, koja se setuje da bude i pozadinska boja cvora
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public ContactsTreeNodeCellRenderer(ClientWindow owner, Color treeColor) {
		this.owner = owner;
		this.treeColor = treeColor;
		
		for (int i = 0; i < icons.length; i++) {
			icons[i] = ImageCreator.createImageIcon(ContactsTreeNodeCellRenderer.class,
											"/status_images/" + STATUS_NAMES[i] + ".png");
		}
		
		onlineIcon = ImageCreator.createImageIcon(ContactsTreeNodeCellRenderer.class,
													"images/online.png");
		
		offlineIcon = ImageCreator.createImageIcon(ContactsTreeNodeCellRenderer.class,
													"images/offline.png");
	}

	
	private Font fontBold = new Font(fontName, Font.BOLD, fontSize);
	private Font fontItalic = new Font(fontName, Font.ITALIC, fontSize);
	
	
	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean sel, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {

		super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

		// postavljanje fonta
		this.setFont(fontBold);
		
		// postavljanje pozadinske boje cvorova stabla
		this.setBackgroundNonSelectionColor(treeColor);
		
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;

		/* Setovanje boje teksta ispisa ClientModelNode cvorova, koji predstavljaju
		 * "Online" i "Offline" cvorove.
		 */
		if ( node.getUserObject() != null ) {
			if (node.getUserObject().equals("Online") ) {
				setIcon(onlineIcon);
				setForeground(Color.BLUE);
			} else
			if ( node.getUserObject().equals("Offline") ) {
				setIcon(offlineIcon);
				setForeground(Color.RED);
			}
		}

		
		/*
		 * Setovanje ikonica za User cvorove, u zavisnosti od njihovog statusa.
		 */
		if ( leaf ) {
			
			User user = (User) node.getUserObject();
			
			switch ( user.getStatus() ) {
				case ONLINE_STATUS:
					setIcon(icons[ONLINE_STATUS]);
					setFont(fontBold);
					break;
				case OFFLINE_STATUS:
					setIcon(icons[OFFLINE_STATUS]);
					setFont(fontItalic);
					break;
				case BUSY_STATUS:
					setIcon(icons[BUSY_STATUS]);
					setFont(fontBold);
					break;
				case AWAY_STATUS:
					setIcon(icons[AWAY_STATUS]);
					setFont(fontBold);
					break;
				case INVISIBLE_STATUS:
					setIcon(icons[OFFLINE_STATUS]);
					setFont(fontItalic);
					break;
			}
			
			if ( owner.getApplicaton().getClientUser().isBlocked(user.getUsername()) ) {
				setIcon(icons[BLOCK_STATUS]);
			}

		}
		
		
		return this;
	}

	
}
